<template>
  <div class="app-container" v-if="!showFile">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="文件夹名" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件夹名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间">
          <el-date-picker
            v-model="queryParams.dateRange"
            style="width: 220px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

     <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件夹id" align="center" prop="id" />
      <el-table-column label="文件夹名" align="center" prop="fileName" />
      <el-table-column label="科室名称" align="center" prop="deptName" />
      <el-table-column label="存储路径" align="center" prop="filePath" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看文件</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="科室" prop="deptId">
          <el-select clearable v-model="form.deptId" style="width:100%">
            <el-option v-for="item in departmentList" :key="item.deptId"  :label="item.deptName" :value="item.deptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存储路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="请输入存储路径" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
  <List :row="row" @onBack="()=>{this.showFile=false}" v-else />
</template>

<script>
import List from './list.vue';
import { listDept } from "@/api/system/dept";
import { folderList,folderCreate,folderDelete } from "@/api/system/dir";
export default {
  components: {
    List
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      //行内容
      row:{},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 显示文件
      showFile: false,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        fileName: undefined,
        dateRange: undefined,
      },
      // 表单参数
      form: {fileName:'',deptId:'',filePath:''},
      // 科室列表
      departmentList:[],
      // 表单校验
      rules: {
        fileName: [
          { required: true, message: "文件夹名称不能为空", trigger: "blur" }
        ],
        filePath: [
          { required: true, message: "文件夹名称存储路径不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "科室不能为空", trigger: ['blur',"change"] }
        ]
      }

    };
  },
  created() {
    this.getList();
    this.getDeptList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      let params=this.queryParams;
      if(params.dateRange){
        params.startDate=params.dateRange[0];
        params.endDate=params.dateRange[1];
        delete params.dateRange;
      }
      folderList(params).then(response => {
        this.postList = response.data || [];
        this.loading = false;
      });
    },
    /** 查询科室列表 */
    getDeptList() {
      this.loading = true;
      listDept({}).then(response => {
        this.departmentList = response.data.filter(item=>item.parentId!==0);
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交表单 */
    submitForm(){
      this.$refs.form.validate(valid => {
        if (valid) {
          //修改
           if(this.row.id){
             folderUpdate({...this.form,id:this.row.id}).then(response => {
                this.$modal.msgSuccess('修改文件夹成功');
                this.open = false;
                this.getList();
             });
             return;
           }
           folderCreate(this.form).then(response => {
               this.$modal.msgSuccess('创建文件夹成功');
              this.open = false;
              this.getList();
            });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.row=selection[0];

    },

    /** 查看 */
    handleView(row) {
      this.row=row;
      this.showFile=true;
    },

    /** 新增 */
    handleAdd(){
      this.row={};
      this.title="新增文件夹";
      this.open=true;

    },
    /** 修改 */
    handleUpdate(){
      if(this.ids.length===0){
        this.$message.warning("请选择要修改的数据");
        return;
      }
      if(this.ids.length>1){
        this.$message.warning("修改只能选择一条数据");
        return;
      }
      this.title="修改文件夹";
      this.form={...this.row};
      this.open=true;

    },

    /** 删除 */
    handleDelete(row){
      if(this.ids.length===0){
        this.$message.warning("请选择要删除的数据");
        return;
      }
      this.$modal.confirm('是否确认删除文件夹"' + this.row.fileName + '"的数据项？').then(() => {
        folderDelete({id:this.row.id}).then(response => {
        this.$modal.msgSuccess("删除成功");
          this.getList();
        });
      })
    },

  }
};
</script>
